<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 用户银行卡提现管理
 *
 * @icon fa fa-circle-o
 */
class Deposit extends Backend
{
    
    /**
     * Deposit模型对象
     * @var \app\admin\model\Deposit
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Deposit;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['user','banking'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $InAuditFell = $this->model
                ->with(['user','banking'])
                ->where($where)
                ->where(['deposit.status'=>array('eq','0')])
                ->order($sort, $order)
                ->sum('deposit.money');


            $AuditPassFeel = $this->model
                ->with(['user','banking'])
                ->where($where)
                ->where(['deposit.status'=>array('eq','1')])
                ->order($sort, $order)
                ->sum('deposit.money');


            $AuditFailureFeel = $this->model
                ->with(['user','banking'])
                ->where($where)
                ->where(['deposit.status'=>array('eq','2')])
                ->order($sort, $order)
                ->sum('deposit.money');

            $list = $this->model
                    ->with(['user','banking'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','account','branch','compellation','type','mobile','province','city','area','createtime','status','cash_fee','money','remark']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['username']);
				$row->visible(['banking']);
				$row->getRelation('banking')->visible(['name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list,"InAuditFell" => $InAuditFell,"AuditPassFeel" => $AuditPassFeel,"AuditFailureFeel" => $AuditFailureFeel,);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function detail($ids){
        $this->view->assign('id',$ids);
        return $this->view->fetch();
    }
    public function up(){
        if ($this->request->isAjax()) {
            $id=$this->request->post('id');
            $status=$this->request->post('status');
            $reviewuser_id=$this->auth->id;
            $posit=$this->model->get(['id' => $id]);
            if($posit['status']==2||$posit['status']==3){
                $this->error('已经审核过了');
                exit;
            }
            $suc=$this->model->upDeposit($id,$status,$reviewuser_id);
            if($suc>0){
                $user=Db::name('user')->where(['id'=>$posit['user_id']])->find();
                if($status==2){
//                    $ti_fee=$posit['money']+$posit['cash_fee'];
//                    $user_status=Db::name('user')->where(['id'=>$posit['user_id']])->setDec('money',$ti_fee);
//                    if($user_status){

//                    }
                    $this->success("审核成功", null, $suc);
                }elseif($status==3){

                    $ti_fee=$posit['money'];

                    Db::name('user')->where(['id'=>$posit['user_id']])->setInc('money',$ti_fee);

//                    if(!empty($posit['moneylog_id'])){
//                        Db::name('user_money_log')->where(['id'=>$posit['moneylog_id']])->update();
//                    }

                    $this->success("提交成功", null, $suc);
                }

            }else{
                $this->success("审核失败", null, $suc);
            }

        }
    }

}
